/*
 * These are the rendering actions library.
 */

var count=0;
var delay=10;
var dist=1;
var width = 400;
var height = 400;


//stores the inforation regarding the coordinates of turtle
var x=200;
var y=200;
var angle=(Math.PI/2);


//fill_values will store the information of rgb values for filling
var rfill_value=256;
var gfill_value=256;
var bfill_value=256;


//stroke_values will store the information of rgb values for stroking
var rstroke_value=0;
var gstroke_value=0;
var bstroke_value=0;

// the turtle image
var turtle;

// drawDelay used by forward, backward, top, down, right, left
var drawDelay = 1;

// Global text area rendered by CodeMirror
var myCodeMirror;

// The first function that is called on html body load
function begin()
{
	// Set focus to the textarea on page load
	myCodeMirror = CodeMirror.fromTextArea(document.getElementById('mytext'), {
	autofocus: true,
        lineNumbers: true,
        matchBrackets: true,
        mode: "text/x-go"});

	var ctx = document.getElementById('canvas').getContext('2d');
	ctx.canvas.width = width; //sets the canvas width
	ctx.canvas.height = height; //sets the canvas height
	ctx.fillStyle = 'rgb(250,250,250)';
	ctx.fillRect(0, 0, width, height);

	ctx = document.getElementById('turtle').getContext('2d');
	ctx.canvas.width = width; //sets the canvas width
	ctx.canvas.height = height; //sets the canvas height
	ctx.clearRect(0, 0, width, height);
        turtle = new Image();
        turtle.onload = function () {
	    ctx.drawImage(turtle, x - 13, y - 17);
        }
	turtle.src = "turtle.png";
}

function redrawTurtle() {
	ctx = document.getElementById('turtle').getContext('2d');
	var w = ctx.canvas.width; //sets the canvas width
	var h = ctx.canvas.height; //sets the canvas height
	ctx.clearRect(0, 0, w, h);
	ctx.drawImage(turtle, x - 13, y - 17);
}

function canvascolor(r, b, g) 
{
	var ctx = document.getElementById('canvas').getContext('2d');
	ctx.fillStyle = 'rgb(' + r + ',' + b + ',' + g + ')';

	ctx.fillRect(0, 0, width, height);
	rfill_value=r; //loads information regarding
	gfill_value=g; //rgb values for filling
	bfill_value=b;
}

function canvassize(w , h)
{
	var ctx = document.getElementById('canvas').getContext('2d');
	ctx.canvas.width=w; //sets the canvas width
	ctx.canvas.height=h; //sets the canvas height
	
	width=w;
	height=h;
	x=width/2;
	y=height/2;
	angle=Math.PI/2;
	ctx.fillStyle = 'rgb(' + rfill_value + ',' + bfill_value + ',' + gfill_value + ')';
	ctx.fillRect(0,0,width,height);

	ctx = document.getElementById('turtle').getContext('2d');
	ctx.canvas.width = w; //sets the canvas width
	ctx.canvas.height = h; //sets the canvas height
	ctx.clearRect(0, 0, w, h);
	ctx.drawImage(turtle, x - 13, y - 17);
}

function pencolor(r , b ,g)
{
	var ctx = document.getElementById('canvas').getContext('2d');
	ctx.strokeStyle = 'rgb(' + r + ',' + b + ',' + g + ')';
	rstroke_value=r;
	gstroke_value=g;
	bstroke_value=b;
}

function penwidth(p_width)
{
	var ctx = document.getElementById('canvas').getContext('2d');
	ctx.lineWidth=p_width; //assigns pen width
}


function forward(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x = x + distance * Math.cos(angle);
		y = y - distance * Math.sin(angle);
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion
        var xdelta = dist * Math.cos(angle);
        var ydelta = dist * Math.sin(angle);

	interpret_pause = 1;
	forwarddraw();


	/*function which draws small lines and calls itself after a delay*/

	function forwarddraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x= x + xdelta;
		y= y - ydelta;
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(forwarddraw, drawDelay); //calls same function after delay of 1ms

		else
		{
			interpret_pause = 0;
			setTimeout(interpret, 0); 
		}//if the required distance has been covered, render is called
	}
}

function backward(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x = x - distance * Math.cos(angle);
		y = y + distance * Math.sin(angle);
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion

	interpret_pause = 1;
	bdraw();


	/*function which draws small lines and calls itself after a delay*/

	function bdraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x=x-dist*Math.cos(angle);
		y=y+dist*Math.sin(angle);
		ctx.lineTo(x,y);
		ctx.stroke();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(bdraw, drawDelay); //calls same function after delay of 1ms

		else { //if the required distance has been covered, render is called
			interpret_pause = 0;
			interpret(); 
		}

	}
}


function turnleft(ang)
{
	ang=((ang*Math.PI)/180); //ang in degrees shouldbe converted to radians
	angle+=ang;

	if(angle>2*Math.PI)
	{
		angle=angle-(2*Math.PI); //to make sure that angle is always between 0 and 360
	}
}


function turnright(ang)
{
	ang=((ang*Math.PI)/180); //ang in degrees shouldbe converted to radians
	angle-=ang;

	if(angle<0)
	{
		angle=angle+2*Math.PI;
	}	
}

function direction(ang)
{
	if(ang<0)
	{
		while(ang>0)
			ang=ang+360;
	}

	if(ang>360)
	{
		while(ang<360)
			ang=ang-360;
	}


	ang=((ang*Math.PI)/180);
	angle=ang;
}

function center() //makes the cursor go to center
{
	x=width/2;
	y=height/2;
}


function go(x_position,y_position)
{
	x=x_position; //goes to the given x,y coordinates
	y=y_position;
}


function gox(x_position)
{
	x=x_position; //goes to given x coordinate keeping y coordinate constant
}

function goy(y_position)
{
	y=y_position;
}

function getx()
{
	return x; //returns x
}

function gety()
{
	return y;
}

function top(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		y = y - distance;
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion

	interpret_pause = 1;
	tdraw();


	/*function which draws small lines and calls itself after a delay*/

	function tdraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');

		ctx.beginPath();
		ctx.moveTo(x,y);
		y=y-dist;
		ctx.lineTo(x,y);
		ctx.stroke();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(tdraw, drawDelay); //calls same function after delay of 1ms

		else { //if the required distance has been covered, render is called
			interpret_pause = 0;
			interpret(); 
		}
	}

}

function down(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		y = y + distance;
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion

	interpret_pause = 1;
	ddraw();


	/*function which draws small lines and calls itself after a delay*/

	function ddraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');

		ctx.beginPath();
		ctx.moveTo(x,y);

		y=y+dist;
		ctx.lineTo(x,y);
		ctx.stroke();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(ddraw, drawDelay); //calls same function after delay of 1ms

		else
		{
			interpret_pause = 0;
			interpret(); 
		}//if the required distance has been covered, render is called
	}

}

function right(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x = x + distance;
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion

	interpret_pause = 1;
	rdraw();


	/*function which draws small lines and calls itself after a delay*/

	function rdraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x=x+dist;
		ctx.lineTo(x,y);
		ctx.stroke();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(rdraw, drawDelay); //calls same function after delay of 1ms

		else
		{
			interpret_pause = 0;
			interpret(); 
		}//if the required distance has been covered, render is called
	}
}

function left(distance)
{
        // If we don't need to animate then we can draw the line directly
        if (drawDelay == 0)
        {
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x = x - distance;
		ctx.lineTo(x,y);
		ctx.stroke();

		// move the turtle forward
		redrawTurtle();
                return;
        }

	var d=0; //variable which is used as a criterion to stop end the forward draw funcion

	interpret_pause = 1;
	ldraw();


	/*function which draws small lines and calls itself after a delay*/

	function ldraw()
	{
		var ctx = document.getElementById('canvas').getContext('2d');
		ctx.beginPath();
		ctx.moveTo(x,y);
		x = x - dist;
		ctx.lineTo(x,y);
		ctx.stroke();

		d+=dist; //calculates how much distance turtle has traveled

		if(d<distance)
			setTimeout(ldraw, drawDelay); //calls same function after delay of 1ms

		else
		{
			interpret_pause = 0;
			interpret(); 
		}//if the required distance has been covered, render is called
	}
}

function clear()
{
	canvassize(width,height);	
}

function getangle()
{
	return angle;
}

function saveAsPNG()
{
		var pngData = document.getElementById('canvas').toDataURL("image/png");
                document.location.href = pngData.replace("image/png", "image/octet-stream");
                //document.getElementById("testing").href = pngData.replace("image/png", "image/octet-stream");
                //document.getElementById("testing").download = "myImage";
}

